


package com.hippo.core.ui.compose.setting

import LoadingDialog
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.state.DialogType
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.KayValueLayout
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.navigation.AppRoute
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.vm.SettingVM

@Composable
fun AccountScreen(
    onBack:()->Unit,
    onLogin:()->Unit,
    toHelp:()->Unit,
    toForgetPW:()->Unit,
    vm: SettingVM = viewModel()) {

    val uiState = vm.uiState

    when(uiState.dialogType){
        DialogType.ResetEmailDialog -> {
            ChangeEmailDialog(
                oldEmail = uiState.userEmail.orEmpty(),
                onDismissRequest = { vm.dismissDialog() },
                onNext = {
                    vm.sendEmailCode(it){
                        vm.updateState(uiState.copy(dialogType = DialogType.EmailCodeDialog))
                    }
                }
            )
        }
        DialogType.EmailCodeDialog -> {
            ChangeEmailCodeDialog(
                email =  uiState.userEmail.orEmpty(),
                onDismissRequest = { vm.dismissDialog() },
                onSure = {
                    vm.changeUserEmail(uiState.emailCode)
                },
                resendCode = {
                    vm.sendEmailCode(it){

                    }
                },
                toHelp = toHelp
            )
        }
        DialogType.DeleteAccountDialog -> {
            DeleteAccountDialog(
                oldEmail =  uiState.userEmail.orEmpty(),
                onDismissRequest = { vm.dismissDialog() },
                onDelete = {
                    vm.deleteAccount{
                        onLogin()
                    }
                }
            )
        }
        is DialogType.LoadingDialog -> {
            LoadingDialog { vm.dismissDialog() }
        }
        else ->{

        }
    }



    CommonBody(
        title = stringResource(R.string.account_1),
        onBack = onBack
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .paddingBody()
        ) {

            VerticalSpacer(10.dp)

            KayValueLayout(
                key = stringResource(R.string.account_2),
                value = uiState.userEmail.orEmpty(),
                rightAlign = true,
                showArrow = true,
                onClick = {
                    vm.updateState(uiState.copy(dialogType = DialogType.ResetEmailDialog))
                }
            )

            KayValueLayout(
                key = stringResource(R.string.account_3),
                value = uiState.password.orEmpty(),
                rightAlign = true,
                showArrow = true,
                onClick = toForgetPW
            )

            VerticalSpacer(36.dp, modifier = Modifier.weight(1F))

            /*
            ThemeButton(
                modifier = Modifier.fillMaxWidth(),
                text = stringResource(R.string.account_4),
                onClick = {
                    vm.updateState(uiState.copy(dialogType = DialogType.DeleteAccountDialog))
                }
            )

             VerticalSpacer(36.dp)
             */

        }
    }

}


@Preview(showBackground = true)
@Composable
private fun AccountScreenPreview() {
    HippoTheme {
        AccountScreen(
            {},{},{},{}
        )
    }
}